package com.xing.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xing.DTO.RoleSaveDTO;

import com.xing.DTO.RoleUpdateDTO;
import com.xing.Utils.Result;
import com.xing.dao.SysRoleDao;
import com.xing.entity.SysRoleEntity;
import com.xing.service.SysRoleService;
import com.xing.utils.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleDao, SysRoleEntity> implements SysRoleService {

    @Autowired
    private SysRoleDao roleDao;

    @Override
    public PageData<SysRoleEntity> listByPage(Integer pageNum, Integer pageSize) {
        // 封装查询条件
        LambdaQueryWrapper<SysRoleEntity> lambdaQueryWrapper = Wrappers.lambdaQuery(SysRoleEntity.class)
                .eq(SysRoleEntity::getDelFlag, 0);
        Page<SysRoleEntity> roleEntityPage = new Page<>(pageNum, pageSize);
        // 查询角色信息
        Page<SysRoleEntity> sysRoleEntityPage = roleDao.selectPage(roleEntityPage, lambdaQueryWrapper);
        // 封装返回信息
        return new PageData<>(sysRoleEntityPage.getRecords(), sysRoleEntityPage.getTotal());
    }

    @Override
    public Result saveRole(RoleSaveDTO roleSaveDTO) {
        // 判断信息是否完善
        if (roleSaveDTO.getName() != null && roleSaveDTO.getDescription() != null) {
            // 复制到对应实体类
            SysRoleEntity sysRoleEntity = BeanUtil.toBean(roleSaveDTO, SysRoleEntity.class);
            // 执行插入操作
            int insert = roleDao.insert(sysRoleEntity);
            if (insert < 1) {
                return new Result().err(456, "插入失败");
            }
            return new Result().ok("插入成功");
        } else {
            return new Result().err(406, "所需信息缺失");
        }
    }

    @Override
    public Result updateById(RoleUpdateDTO roleUpdateDTO) {
        // 判断必要条件是合法
        if (roleUpdateDTO.getId() == null) {
            return new Result().err(406, "所需信息缺失");
        }
        // 执行修改操作
        int update = roleDao.updateById(BeanUtil.toBean(roleUpdateDTO, SysRoleEntity.class));
        if (update < 1) {
            return new Result().err(456, "修改失败");
        }
        return new Result().ok("修改成功");
    }

    @Override
    public Result deleteById(Long id) {
        SysRoleEntity sysRoleEntity = roleDao.selectById(id);
        if (sysRoleEntity == null) {
            return new Result().err(457, "无效id");
        } else {
            sysRoleEntity.setDelFlag(1);
            int update = roleDao.updateById(sysRoleEntity);
            if (update < 1) {
                return new Result().err(456, "删除失败");
            }
            return new Result().ok("删除成功");
        }
    }

    @Override
    public List<SysRoleEntity> listAll() {
        LambdaQueryWrapper<SysRoleEntity> lambdaQueryWrapper = Wrappers.lambdaQuery(SysRoleEntity.class)
                .eq(SysRoleEntity::getDelFlag, 0);
        return roleDao.selectList(lambdaQueryWrapper);
    }


}
